package bz.ziro.kanbe.controller.mng.log;

import java.util.Date;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import bz.ziro.kanbe.model.Log;
import bz.ziro.kanbe.util.KeyFactory;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * ログの追加
 * @author Administrator
 */
public class AddController extends Controller {

    @Override
    public Navigation run() {

        Log log = new Log();
        //データを設定
        BeanUtil.copy(request, log);
       
        String siteKeyBuf = sessionScope("siteKey");
        String pageKeyBuf = requestScope("logPageKey");
 
        Long siteLngKey = Long.valueOf(siteKeyBuf);
        Key siteKey = KeyFactory.createSiteKey(siteLngKey);
        //キー作成
        Key pageKey = KeyFactory.createPageKey(siteLngKey,Long.valueOf(pageKeyBuf));
 
        //各種キー取得
        log.setSiteKey(siteKey);
        log.setPageKey(pageKey);

        //作成者情報を取得
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        log.setCreateDate(new Date());
        log.setCreator(user);

        Key key = KeyFactory.allocateLogKey();
        log.setKey(key);
        Datastore.put(log);
    	
        return null;
    }
}
